<?php

/**
 webpos 角色位置管理  yjmp
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require(dirname(__FILE__) . '/includes/lib_yjmp.php');

/* act操作项的初始化 */
if (empty($_REQUEST['act']))
{
    $_REQUEST['act'] = 'login';
}
else
{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

/* 初始化 $exc 对象 */
$exc = new exchange($ecs->table("webpos_location"), $db, 'location_id', 'location_name');



/*------------------------------------------------------ */
//-- 登陆界面
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'login')
{
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache");

    if ((intval($_CFG['captcha']) & CAPTCHA_ADMIN) && gd_version() > 0)
    {
        $smarty->assign('gd_version', gd_version());
        $smarty->assign('random',     mt_rand());
    }

    $smarty->display('login.htm');
}


/*------------------------------------------------------ */
//-- 位置列表页面
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'list')
{
    /* 模板赋值 */
    $smarty->assign('ur_here',     '店员位置');
    $smarty->assign('action_link', array('href'=>'webpos_location.php?act=add', 'text' =>'添加位置'));
    $smarty->assign('full_page',   1);
    $smarty->assign('admin_list',  get_location_list());

    /* 显示页面 */
    assign_query_info();
    $smarty->display('webpos_location_list.htm');
}

/*------------------------------------------------------ */
//-- 查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $smarty->assign('admin_list',  get_location_list());

    make_json_result($smarty->fetch('webpos_location_list.htm'));
}

/*------------------------------------------------------ */
//-- 添加位置页面
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'add')
{
    /* 检查权限 */
    admin_priv('admin_manage');
    include_once(ROOT_PATH . 'languages/' .$_CFG['lang']. '/admin/priv_action.php');

     /* 模板赋值 */
    $smarty->assign('ur_here',     '添加位置');
    $smarty->assign('action_link', array('href'=>'webpos_location.php?act=list', 'text' => '位置列表'));
    $smarty->assign('form_act',    'insert');
    $smarty->assign('action',      'add');
    $smarty->assign('lang',        $_LANG);
    $smarty->assign('priv_arr',    $priv_arr);

    /* 显示页面 */
    assign_query_info();
    $smarty->display('webpos_location_info.htm');
}

/*------------------------------------------------------ */
//-- 添加位置的处理
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'insert')
{
    admin_priv('admin_manage');
    $sql = "INSERT INTO ".$ecs->table('webpos_location')." (location_name, location_address, location_describe) ".
           "VALUES ('".trim($_POST['location_name'])."','".trim($_POST['location_address'])."','".trim($_POST['location_describe'])."')";
    $db->query($sql);
    /* 转入权限分配列表 */
    $new_id = $db->Insert_ID();
    /*添加链接*/

    $link[0]['text'] = '店员位置列表';
    $link[0]['href'] = 'webpos_location.php?act=list';

    sys_msg($_LANG['add'] . "&nbsp;" .$_POST['location_name'] . "&nbsp;" . $_LANG['action_succeed'],0, $link);

 }

/*------------------------------------------------------ */
//-- 编辑位置信息
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit')
{
    include_once(ROOT_PATH . 'languages/' .$_CFG['lang']. '/admin/priv_action.php');
    $_REQUEST['id'] = !empty($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;

    /* 获取角色信息 */
    $sql = "SELECT location_id, location_name, location_describe, location_address FROM " .$ecs->table('webpos_location').
           " WHERE location_id = '".$_REQUEST['id']."'";
    $user_info = $db->getRow($sql);

    /* 模板赋值 */

    $smarty->assign('user',        $user_info);
    $smarty->assign('form_act',    'update');
    $smarty->assign('action',      'edit');
    $smarty->assign('ur_here',     $_LANG['admin_edit_role']);
    $smarty->assign('action_link', array('href'=>'webpos_location.php?act=list', 'text' => '位置列表'));
    $smarty->assign('lang',        $_LANG);
    $smarty->assign('user_id',     $_GET['id']);

    assign_query_info();
    $smarty->display('webpos_location_info.htm');
}

/*------------------------------------------------------ */
//-- 更新位置信息
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'update')
{
    /* 更新管理员的权限 */
    $sql = "UPDATE " .$ecs->table('webpos_location'). " SET location_address = '".$_POST['location_address']."', location_name = '".$_POST['location_name']."', location_describe = '".$_POST['location_describe']." ' ".
           "WHERE location_id = '$_POST[id]'";
    $db->query($sql);
    /* 提示信息 */
    $link[] = array('text' => '返回位置列表', 'href'=>'webpos_location.php?act=list');
    sys_msg($_LANG['edit'] . "&nbsp;" . $_POST['user_name'] . "&nbsp;" . $_LANG['action_succeed'], 0, $link);
}

/*------------------------------------------------------ */
//-- 删除一个角色
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
    check_authz_json('admin_drop');

    $id = intval($_GET['id']);
    $num_sql = "SELECT count(*) FROM " .$ecs->table('users'). " WHERE location_id = '$_GET[id]'";
    $remove_num = $db->getOne($num_sql);
    if($remove_num > 0)
    {
        make_json_error('此位置有用户在使用,无法删除');
    }
    else
    {
        $exc->drop($id);
        $url = 'webpos_location.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
    }
    ecs_header("Location: $url\n");
    exit;
}
?>
    